/**
 * 贴心关怀
 * @author truexin
 * @date   16/08/18
 */
import {health} from "../const/health";
import {pension} from "../const/pension";
import PullRefresh from '../components/mui/PullRefresh.js';

(function (lf, r) {
    
    let ajax = lf.utils.ajax,
        getUrlParam = lf.utils.getUrlParam,
        content = '', //用于输入温馨文字的内容
        fmt = 'yyyy.MM.dd',  //原来有时间显示
        formatTime = lf.utils.format;
    
    let userId = getUrlParam('userId');

    var Sendor = r.createClass({
        getInitialState: function () {
            return ({
                disabled: false,
            })
        },
        checkSubmit: function () {
            content = $(r.findDOMNode(this.refs.input)).val();
            if (!$.trim(content)) return false;  //注意这里输入空格默认为存在value文本
            return content;
        },
        submit: function () {
            var me = this;
            if (me.checkSubmit()) {
                me.setState({
                    disabled: true,
                });
                ajax({
                        type: 'post',
                        url: health.api.prefix + health.api.sendMsg,
                        data: {
                            projectId: me.props.user.projectId,
                            houseNum: me.props.houseNum,

                            familyId: '',
                            clientId: '',
                            boxGlobalId: '',

                            userId: userId,
                            message: content,
                        }
                    }, null, () => {
                        ajax({
                                type: 'post',
                                url: pension.api.prefix + pension.api.create,
                                data: {
                                    mobileSend: me.props.user.mobile,
                                    mobileRecipient: me.props.mobile,
                                    content: content,
                                }
                            }, null, () => {
                                lf.appJs.toast('发送成功');
                                location.reload();
                            }
                        )
                    }
                )
            } else {
                lf.appJs.toast('请先填写温馨提醒');
            }
        },

        render: function () {
            return (
                <div className="send-btns">
                    <ul>
                        <li><input type="text" ref='input' placeholder="请输入温馨提醒..."/></li>
                        <li>
                            <button disabled={this.state.disabled} type="submit" onClick={this.submit}></button>
                        </li>
                    </ul>
                </div>
            )
        }
    });

    let Body = r.createClass({

        render: function () {
            let data = this.props.state, //接口返回数组,
                max = this.props.maxStandard,
                min = this.props.minStandard,
                diastolicPress = '', //对应的数值
                temperature = '',
                waterPercent = '',
                fatPercent = '',
                weight = '',
                systolicPress = '',

                active = { //判断是否异常
                    weight: '',
                    fat: '',
                    temperature: '',
                    Press: '',
                },

                name = this.props.name, //title 位置用户名字显示
                tipsList = []; //用于存温馨提醒消息

            $.each(data, function (i) {

                this.warmTips ? tipsList.push(this.warmTips) : '';

                if (this.type == health.type.blood) {
                    diastolicPress = this.diastolicPress;
                    systolicPress = this.systolicPress;
                    this.result != "NORMAL" ? active.press = true : active.press = false;
                }
                if (this.type == health.type.temperature) {
                    temperature = this.temperature;
                    this.result != "NORMAL" ? active.temperature = true : active.temperature = false;
                }
                if (this.type == health.type.fat) {
                    fatPercent = this.fatPercent;
                    waterPercent = this.waterPercent;
                    this.result != "NORMAL" ? active.fat = true : active.fat = false;
                }
                if (this.type == health.type.weight) {
                    weight = this.weight;
                    this.result != "NORMAL" ? active.weight = true : active.weight = false;
                }
            })

            return (
                <div className="data">
                    <p className="type name title">{name}</p>
                    <p className="type title">最新数据</p>
                    <p className={"type " + (weight && (weight>max||weight<min) ? 'active' : '')}>
                        {'体重( KG ) : ' + weight}
                        <i className={weight && weight>max ? 'mui-icon mui-icon-arrowthinup'
                        :
                        (weight && weight<min ? 'mui-icon mui-icon-arrowthindown' :
                        '')}></i>
                    </p>
                    <p className={"type " + (temperature && (temperature>37.2||temperature<36) ? 'active' : '')}>
                        {'体温( ℃ ) : ' + temperature}
                        <i className={temperature && temperature>37.2 ? 'mui-icon mui-icon-arrowthinup'
                        :
                        (temperature && temperature<36 ? 'mui-icon mui-icon-arrowthindown' :
                        '')}></i>
                    </p>
                    <p className={"type " + (diastolicPress && (diastolicPress>90||diastolicPress<60) ? 'active':'')}>
                        {'舒张压( mmHg ) : ' + diastolicPress}
                        <i className={diastolicPress && diastolicPress>90 ? 'mui-icon mui-icon-arrowthinup'
                        :
                        (diastolicPress && diastolicPress<60 ? 'mui-icon mui-icon-arrowthindown' :
                        '')}></i>
                    </p>
                    <p className={"type " + (systolicPress && (systolicPress>140||systolicPress<90) ? 'active' : '')}>
                        {'收缩压( mmHg ) : ' + systolicPress}
                        <i className={systolicPress && systolicPress>140 ? 'mui-icon mui-icon-arrowthinup'
                        :
                        (systolicPress && systolicPress<90 ? 'mui-icon mui-icon-arrowthindown' :
                        '')}></i>
                    </p>
                    <p className={"type " + (waterPercent && (waterPercent>52.7||waterPercent<49.5) ? 'active' : '')}>
                        {'水分率( % ) : ' + waterPercent}
                        <i className={waterPercent && waterPercent>52.7 ? 'mui-icon mui-icon-arrowthinup'
                        :
                        (waterPercent && waterPercent<49.5 ? 'mui-icon mui-icon-arrowthindown' :
                        '')}></i>
                    </p>
                    <p className={"type " + (fatPercent && (fatPercent>34||fatPercent<21) ? 'active' : '')}>
                        {'脂肪率( % ) : ' + fatPercent}
                        <i className={fatPercent && fatPercent>34 ? 'mui-icon mui-icon-arrowthinup'
                        :
                        (fatPercent && fatPercent<21 ? 'mui-icon mui-icon-arrowthindown' :
                        '')}></i>
                    </p>
                </div>
            );
        }
    });
    let Tips = r.createClass({

        render: function () {
            let me = this,
                warmTipsList = me.props.warmTipsList,
                sendWarmTips = [ //根据产品提示的文案进行备存温馨提示数据
                    "体重超标,该减肥啦!",
                    "体温有点高，多喝水,尽早去医院",
                    "血压过高,注意饮食清淡",
                    "最近健康状况很好，继续保持",
                ],
                tipsList = warmTipsList.map(function (tips) {
                    return (
                        <p >{tips}</p>
                    )
                })

            let sendList = sendWarmTips.map(function (tips) {
                return (
                    <div className="mui-table-view-cell flex sending">
                        <p className="flex-item content">{tips}</p>
                        <p className="flex-item tipBtn">提醒他</p>
                    </div>
                )
            })
            return (
                <div className="tips">
                    <p >温馨提醒:</p>
                    {tipsList}
                    <div className="sendList">
                        {sendList}
                    </div>
                </div>
            );
        }
    });
    
    let Root = r.createClass({
        getInitialState: function () {
            return {
                warmTipsList: [], //用于系统温馨提示存蓄
                data: {}, //接口返回的数据
                name: '',
                mobile: '',
                houseNum: '',
                maxStandard: '',
                minStandard: '',
            }
        },
        //获取Name
        getName(){
            let me = this;
            ajax({
                type: "post",
                data: {
                    userId: userId
                },
            }, health.api.queryInfo, (data) => {
                me.setState({
                    name: data.nickName,
                    mobile: data.mobile,
                })
            });
        },
        //获取houseNum
        getHouserInfo(user){
            let me = this;
            ajax({
                type: "get",
                data: {
                    houseId: user.houseId
                },
            }, lf.finger.api.household.getHouseInfo, (data) => {
                me.setState({
                    houseNum: data.houseNum
                })
            });
        },
        getWeightValue() {
            let me = this;
            ajax({
                    type: 'get',
                    url: health.api.prefix + health.api.standard,
                    data: {
                        userId: userId,
                        type: 6 //体重
                    }
                }, null, data => {
                    me.setState({
                        maxStandard: data.maxStandard,
                        minStandard: data.minStandard
                    })

                }
            )
        },
        componentWillMount: function () {
            let me = this;
            me.getName();
            me.getHouserInfo(me.props.user);
            me.getWeightValue();
            ajax({
                    type: 'get',
                    url: health.api.prefix + health.api.queryDetail,
                    data: {
                        userId: userId,
                    }
                }, null, data => {
                    let tipsList = [];

                    $.each(data, function (i) {
                        this.warmTips ? tipsList.push(this.warmTips) : '';
                    })

                    me.setState({
                        data: data,
                        warmTipsList: tipsList
                    })
                }
            )
        },
        componentDidMount: function () {
            let me = this,
                $list = $('.sending'),
                user = me.props.user;

            $('.sendMsg').on('focus', function () {
                $('.sendList').show();
            })

            $list.each(function (i) {
                var $me = $(this);
                $me.off();

                $me.on('tap', function () {
                    let content = $me.find('.content').text();

                    if (!$me.find('.sent').length && content) {
                        ajax({
                                type: 'post',
                                url: health.api.prefix + health.api.sendMsg,
                                data: {
                                    projectId: user.projectId,
                                    houseNum: me.state.houseNum,

                                    familyId: '',
                                    clientId: '',
                                    boxGlobalId: '',

                                    userId: userId,
                                    message: content,
                                }
                            }, null, () => {
                                ajax({
                                        type: 'post',
                                        url: pension.api.prefix + pension.api.create,
                                        data: {
                                            mobileSend: user.mobile,
                                            mobileRecipient: me.state.mobile,
                                            content: content,
                                        }
                                    }, null, () => {
                                        lf.appJs.toast('发送成功');
                                        $me.find('.tipBtn').addClass('sent');
                                    }
                                )
                            }
                        )
                    } else {
                        lf.appJs.toast('请选择温馨提醒');
                    }
                });
            })

        },
        render: function () {
            return <div className="love-care">
                <PullRefresh data={
                    <div>
                        <Body state={this.state.data}
                      user={this.props.user}
                      name={this.state.name}
                      maxStandard={this.state.maxStandard}
                      minStandard={this.state.minStandard}/>
                      <Tips warmTipsList={this.state.warmTipsList}/>
                    </div>
                }
                />
                <Sendor user={this.props.user} houseNum={this.state.houseNum} mobile={this.state.mobile}/>
            </div>
        }
    });
    
    lf.appJs.getUser(function (user) {
        r.render(
            <Root user={user}/>,
            document.getElementById('root'), function () {
                $('#root').css({backgroundColor: '#f2f2f2'});
                $('.mui-scroll ').css({backgroundColor: '#f2f2f2'});
                $('.mui-scroll-wrapper').css({
                    'bottom': '.5rem'
                })
            }
        )
    });
})(liefeng, React)
